package com.admission.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.admission.communicate.KeyTools;
import com.admission.communicate.OrderBuilder;
import com.admission.model.Order;
import com.admission.model.OrderTemp;
import com.admission.model.PayLog;
import com.admission.service.OrderTempManager;
import com.admission.service.PayLogManager;
import com.admission.util.DateUtils;
import com.admission.util.MoneyUtils;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class PayOrderAction extends CrudActionSupport<OrderTemp>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8784111971389869175L;
	HttpServletRequest request=ServletActionContext.getRequest();
	private PayLog payLog;
	private PayLogManager payLogManager;
	private OrderTempManager orderTempManager;
	private OrderTemp orderTemp;
	private String RMB;
	private Order order;

	public PayLog getPayLog() {
		return payLog;
	}
	public void setPayLog(PayLog payLog) {
		this.payLog = payLog;
	}
	public PayLogManager getPayLogManager() {
		return payLogManager;
	}
	public void setPayLogManager(PayLogManager payLogManager) {
		this.payLogManager = payLogManager;
	}
	public OrderTempManager getOrderTempManager() {
		return orderTempManager;
	}
	public void setOrderTempManager(OrderTempManager orderTempManager) {
		this.orderTempManager = orderTempManager;
	}
	public OrderTemp getOrderTemp() {
		return orderTemp;
	}
	public void setOrderTemp(OrderTemp orderTemp) {
		this.orderTemp = orderTemp;
	}

	public String getRMB() {
		return RMB;
	}
	public void setRMB(String display) {
		this.RMB = display;
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	
	@Override
	public String list() throws Exception {
		//加入订单临时表
		String idNum = request.getSession().getAttribute("USER_IDNUM").toString();
		payLog = payLogManager.getPayLogByIdNum(idNum);
		if(payLog.getPayStatus().equals("1")){
			this.addActionMessage("该考生已缴费");
			return "pay-order";
		}
		orderTemp = orderTempManager.getOrderTempByIdNum(idNum);
		if(orderTemp != null){
			String mobile1 = orderTemp.getMobile();
			String mobile2 = payLog.getMobile();
			if(!mobile1.equals(mobile2)){
				this.addActionMessage("您已加入集体缴费名单，不能在此缴费");
				return "pay-order";
			}
		}
		else {
			String dateString = DateUtils.getTimeString();
			
			orderTemp = new OrderTemp();
			//个人缴费时，用考生号加时间时分秒拼接订单号
			orderTemp.setOrderId(request.getSession().getAttribute("USER_EXAMNO").toString() + dateString);
			orderTemp.setMobile(payLog.getMobile());
		    orderTemp.setExamineeNo(payLog.getExamieeNo());
		    orderTemp.setIdnum(payLog.getIdnum());
		    orderTemp.setName(payLog.getName());
		    orderTemp.setAmount(payLog.getAmount());
		    orderTempManager.save(orderTemp);
		}
			
		//创建订单
		order = OrderBuilder.CreatOrder();		
		//String orderID = ;
		order.setOrdId(orderTemp.getOrderId()); 
		order.setShareData(order.getShareAccount() + "^" + order.getOrdAmt());
		String signString = KeyTools.GenSignString(orderTemp.getOrderId(), order.getOrdAmt());
		order.setChkValue(signString);
		//保存订单明文，返回支付结果时做验证
		SavePlainData(order);
		
		//显示订单总额
		setRMB(MoneyUtils.stringToMoney(order.getOrdAmt()));
		
		return "pay-order";
	}

	private void SavePlainData(Order order) {
		String plainData = order.getMerId() + order.getOrdId() + order.getOrdAmt() + order.getCuryId() + order.getVersion()
		+ order.getBgRetUrl() + order.getPageRetUrl() + order.getShareType() + order.getShareData();
		
		HttpSession session=request.getSession(false);
	      session.setAttribute("PLAINDATA", plainData);
	      
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OrderTemp getModel() {
		// TODO Auto-generated method stub
		return null;
	}
}
